<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程分类控制器
 */
class Category extends Backend
{
    /**
     * 当前控制器下的一个模型属性
     */
    protected $model = null;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        // 引入的模型
        $this->model = model('Subject.Category');
    }

    // public function index()
    // {
    //     //设置过滤方法
    //     $this->request->filter(['strip_tags']);

    //     if ($this->request->isAjax()) {
            
    //         if ($this->request->request('keyField')) {
    //             return $this->selectpage();
    //         }

    //         list($where, $sort, $order, $offset, $limit) = $this->buildparams();

    //         $total = $this->model
    //             ->where($where)
    //             ->order($sort, $order)
    //             ->limit($offset, $limit)
    //             ->count();  //查询总数

    //         $list = $this->model
    //             ->where($where)
    //             ->order($sort, $order)
    //             ->limit($offset, $limit)
    //             ->select();  //查询数据

    //         $result = array("total" => $total, "rows" => $list);

    //         return json($result);
    //     }
    //     return $this->view->fetch();
    // }

    public function add()
     {
        if($this->request->isPost())
        {
            $params = $this->request->param('row/a');

            //插入数据库
            $result = $this->model->validate('common/subject/category')->save($params);

            if($result === FALSE)
            {
                $this->error($this->model->getError());
                exit;
            }else
            {
                $this->success();
                exit;
            }
        }
        return $this->view->fetch();
     }

    public function edit($ids = null)
    {
        $row = $this->model->find($ids);

        if (!$row) {
            $this->error(__('No Results were found'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params['id'] = $ids;
                $result = $this->model->validate("Common/Subject/Category")->isUpdate(true)->save($params);

                if ($result === false) {
                    $this->error($this->model->getError());
                    exit;
                }

                $this->success();
                exit;
            }
        }

        $this->assign("row", $row);

        return $this->view->fetch();
    }

    
}
